require('dotenv').config();
// 引入polyfills以支持旧版本Node.js
require('./utils/polyfills');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
const logger = require('morgan');
const adminAuth = require('./middlewares/admin-auth');
const userAuth = require('./middlewares/user-auth');

// 启动邮件消费者
const { mailConsumer } = require('./utils/rabbit-mq');
const cors = require('cors');
const captcha = require('./middlewares/captcha');
const initScheduleTasks = require('./tasks');
const routers = [
  /**通用 */
  { router: require('./routes/upload'), path: '/upload', middlewares: [adminAuth] },
  { router: require('./routes/captcha'), path: '/captcha', middlewares: [] },
  /**后台 */
  { router: require('./routes/admin/charts'), path: '/admin/charts', middlewares: [adminAuth] },
  { router: require('./routes/admin/orders'), path: '/admin/orders', middlewares: [adminAuth] },
  {
    router: require('./routes/admin/memberships'),
    path: '/admin/memberships',
    middlewares: [adminAuth],
  },
  {
    router: require('./routes/admin/attachment'),
    path: '/admin/attachment',
    middlewares: [adminAuth],
  },
  { router: require('./routes/admin/logs'), path: '/admin/logs', middlewares: [adminAuth] },
  { router: require('./routes/admin/articles'), path: '/admin/articles', middlewares: [adminAuth] },
  {
    router: require('./routes/admin/categories'),
    path: '/admin/categories',
    middlewares: [adminAuth],
  },
  { router: require('./routes/admin/settings'), path: '/admin/settings', middlewares: [adminAuth] },
  { router: require('./routes/admin/users'), path: '/admin/users', middlewares: [adminAuth] },
  { router: require('./routes/admin/courses'), path: '/admin/courses', middlewares: [adminAuth] },
  { router: require('./routes/admin/chapters'), path: '/admin/chapters', middlewares: [adminAuth] },
  { router: require('./routes/admin/auth'), path: '/admin/auth', middlewares: [captcha] },
  /**前台 */
  { router: require('./routes/user/index'), path: '/user/index', middlewares: [] },
  { router: require('./routes/user/categories'), path: '/user/categories', middlewares: [] },
  { router: require('./routes/user/courses'), path: '/user/courses', middlewares: [] },
  { router: require('./routes/user/chapters'), path: '/user/chapters', middlewares: [userAuth] },
  { router: require('./routes/user/articles'), path: '/user/articles', middlewares: [] },
  { router: require('./routes/user/settings'), path: '/user/settings', middlewares: [userAuth] },
  { router: require('./routes/user/search'), path: '/user/search', middlewares: [userAuth] },
  { router: require('./routes/user/auth'), path: '/user/auth', middlewares: [captcha] },
  { router: require('./routes/user/user'), path: '/user/user', middlewares: [userAuth] },
  { router: require('./routes/user/like'), path: '/user/like', middlewares: [userAuth] },
  { router: require('./routes/user/orders'), path: '/user/order', middlewares: [userAuth] },
  { router: require('./routes/user/alipay'), path: '/user/alipay', middlewares: [] },
];

var app = express();

// const corsOptions = {
//     /**允许这两个源跨域请求 */
//     origin: [
//         'https://clwy.cn',
//         'http://127.0.0.1:5501'
//     ],
// }
// app.use(cors(corsOptions));
// CORS 跨域配置
app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

(async () => {
  await mailConsumer();
})();
initScheduleTasks(); //加载定时任务

// 前台路由配置
routers.forEach((item) => app.use(item.path, ...item.middlewares, item.router));

module.exports = app;
